local yeshen = fk.CreateSkill{
  name = "rfenghou_3d14j__yeshen",
}

yeshen:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yeshen.name) and target.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getMark("@rfenghou_3d14j__yeshen")
    local ids = room:getNCards(x)
    room:turnOverCardsFromDrawPile(player, ids, yeshen.name)
    local blacks = table.filter(ids, function (id) return Fk:getCardById(id).color == Card.Black end)
    if #blacks > 0 then
      local c = room:askToCards(target, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = yeshen.name,
        cancelable = false,
        pattern = ".|.|.|.|.|.|" .. table.concat(blacks, ","),
        prompt = "#rfenghou_3d14j__yeshen-choose",
        expand_pile = blacks
      })
      if #c == 1 then
        local y = Fk:translate(Fk:getCardById(c[1]).trueName, "zh_CN"):len()
        local to = room:askToChoosePlayers(target, {
          targets = table.filter(room:getAllPlayers(), function(p)
            return target:canUseTo(Fk:cloneCard("iron_chain"), p) end),
          min_num = y,
          max_num = y,
          prompt = "#rfenghou_3d14j__yeshen-use:::"..y,
          skill_name = yeshen.name,
          cancelable = true
        })
          if #to == y and y > 0 then
            local card = Fk:cloneCard("iron_chain")
            card.skillName = yeshen.name
            card:addSubcard(Fk:getCardById(c[1]))
            local use = {
              from = target,
              tos = to,
              card = card,
              extraUse = true,
            }
            room:useCard(use)
          else
            room:moveCards({
              ids = {c[1]},
              from = target,
              toArea = Card.DiscardPile,
              skillName = yeshen.name,
              moveReason = fk.ReasonRecast,
              proposer = target,
            })
            if not target.dead then
              target:drawCards(1, "#RecastBySkill")
            end
          end
          local blacksleft = table.filter(ids, function (id) return room:getCardArea(id) == Card.Processing end)
          if #blacksleft > 0 and not target.dead then
            blacksleft = room:askToGuanxing(target, {
              cards = blacksleft,
              top_limit = { #blacksleft, #blacksleft },
              bottom_limit = { 0, 0 },
              skill_name = yeshen.name,
              skip = true
            }).top
            blacksleft = table.reverse(blacksleft)
          end
          room:moveCards({
            ids = blacksleft,
            from = target,
            toArea = Card.DrawPile,
            moveReason = fk.ReasonJustMove,
            skillName = yeshen.name,
            proposer = target,
          })
        room:removePlayerMark(player, "@rfenghou_3d14j__yeshen", 1)
      end
      if not player.dead and player:getMark("@rfenghou_3d14j__yeshen") == 0 then
        room:setPlayerMark(player, "@rfenghou_3d14j__yeshen", 3)
        room:damage {
          from = player,
          to = player,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = yeshen.name
        }
      end
    else
      room:setPlayerMark(player, "@rfenghou_3d14j__yeshen", 3)
      room:damage {
        from = player,
        to = player,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = yeshen.name
      }
    end
    room:cleanProcessingArea(ids, yeshen.name)
  end,
})

yeshen:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@rfenghou_3d14j__yeshen", 3)
end)

yeshen:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@rfenghou_3d14j__yeshen", 0)
end)


Fk:loadTranslationTable{
  ["rfenghou_3d14j__yeshen"] = "冶身",
  [":rfenghou_3d14j__yeshen"] = "一名角色的结束阶段，你可以亮出牌堆底三张牌，若其中有黑色牌，其将其中一张当额定目标数为牌名字数的【铁索连环】使用或重铸，令“冶身”亮出牌数-1，"..
  "然后将其余牌置于牌堆顶，若没有黑色牌或亮出牌数因此减至0，你复原“冶身”并对自己造成1点火焰伤害。",

  ["@rfenghou_3d14j__yeshen"] = "冶身",
  ["#rfenghou_3d14j__yeshen-choose"] = "冶身：请选择一张用于转化【铁索连环】或重铸的牌！（若使用，额定目标数为牌名字数，否则重铸！）",
  ["#rfenghou_3d14j__yeshen-use"] = "冶身：请选择 %arg 名角色作为【铁索连环】的目标，否则重铸先前选择的牌！",
}

return yeshen
